using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Weazel.Toad.Application.Forms
{
  public partial class VhdlConversionFailedForm : Weazel.Toad.Application.Forms.DialogBaseForm
  {
    private Exception currentException = null;
    private Stack<Exception> previousExceptions = new Stack<Exception>();

    private Exception exception;
    public Exception Exception
    {
      get { return exception; }
      set { exception = value; }
    }

    public VhdlConversionFailedForm()
    {
      InitializeComponent();
    }

    private string formatText(Exception e)
    {
      StringBuilder str = new StringBuilder();
      str.Append(e.Message);
      str.AppendLine();
      str.Append(e.StackTrace);
      return str.ToString();
    }

    private void updateGui()
    {
      richTextBox1.Text = formatText(currentException);

      if (currentException.InnerException != null)      
        nextExceptionButton.Enabled = true;      
      else      
        nextExceptionButton.Enabled = false;      

      if (previousExceptions.Count > 0)
        previousExceptionButton.Enabled = true;
      else
        previousExceptionButton.Enabled = false;
    }

    protected override void OnLoad(EventArgs e)
    {
      base.OnLoad(e);

      if (exception == null)
        throw new NullReferenceException("Exception not set");

      currentException = exception;
      updateGui();
    }

    private void nextExceptionButton_Click(object sender, EventArgs e)
    {
      if (currentException.InnerException != null)
      {
        previousExceptions.Push(currentException);
        currentException = currentException.InnerException;
        updateGui();
      }
    }

    private void previousExceptionButton_Click(object sender, EventArgs e)
    {
      if (previousExceptions.Count > 0)
      {
        currentException = previousExceptions.Pop();
        updateGui();
      }
    }
  }
}

